<?php

namespace app\admin\service;

use app\admin\model\UM as UM;
use app\common\model\ConsultingServer;
use think\exception\ValidateException;

/**
 * Class PresetConsultingServerService
 * @package app\chat\service
 */
class ConsultingPresetServerService
{

    /**
     * 查询预设服务
     * @param \app\admin\model\UM|null $user
     * @param $paramsType
     * @return ConsultingServer|null
     */
    public function read(?UM $user, $paramsType): ?ConsultingServer
    {
        if (!is_numeric($paramsType) || $paramsType <= 1) {
            throw new ValidateException('请选择正确的服务');
        }

        return ConsultingServer::where('serverType', $paramsType)
            ->with(['recommendServer' => fn($query) => $query->append(['iconImg'])])
            ->append(['iconImg'])
            ->find();
    }

    /**
     * 修改
     * @param \app\admin\model\UM $user
     * @param $id
     * @param array $params
     * @return void
     */
    public function modify(UM $user, $id, array $params): void
    {
        $record = ConsultingServer::where('id', $id)
            ->find();

        if (is_null($record)) {
            throw new ValidateException('该服务不存在');
        }

        if (!is_numeric($record->serverType) || $record->serverType <= 1) {
            throw new ValidateException('该接口不能保存自定义服务');
        }

        if ($record->serverType == '2') {

            if (!isset($params['maxTimes']) || !is_numeric($params['maxTimes']) || $params['maxTimes'] < 0) {
                throw new ValidateException('请设置正确的免费次数');
            }
            if (!isset($params['questionsNum']) || !is_numeric($params['questionsNum']) || $params['questionsNum'] < 0) {
                throw new ValidateException('请设置正确的免费聊天次数');
            }
            if(!isset($params['subsidy']) || !is_numeric($params['subsidy']) || $params['subsidy'] < 0){
                throw new ValidateException('请输入正确的补贴金额');
            }

            $record->maxTimes = $params['maxTimes'];
            $record->questionsNum = $params['questionsNum'];
            $record->subsidy = $params['subsidy'];
            $record->recommendServerId = $params['recommendServerId'];
            $record->save();
        } else {

            if (is_blank($params['icon'] ?? null)) {
                throw new ValidateException('请上传图标');
            }
            if (is_blank($params['name'] ?? null)) {
                throw new ValidateException('请输入服务名称');
            }
            $params['chatFreelyDuration'] ??= null;
            if (!is_numeric($params['chatFreelyDuration']) || $params['chatFreelyDuration'] < 0) {
                throw new ValidateException('请输入正确的时长');
            }
            $params['showPrice'] ??= null;
            if (is_blank($params['showPrice']) || !is_numeric($params['showPrice']) || $params['showPrice'] < 0) {
                throw new ValidateException('请输入正确的原价');
            }
            $params['price'] ??= null;
            if (is_blank($params['price']) || !is_numeric($params['price']) || $params['price'] < 0) {
                throw new ValidateException('请输入正确的现价');
            }

            $record->name = $params['name'];
            $record->chatFreelyDuration = $params['chatFreelyDuration'];
            $record->icon = $params['icon'];
            $record->showPrice = $params['showPrice'];
            $record->price = $params['price'];
            $record->save();
        }
    }

}
